package com.lws.basic.base;

import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lws.basic.utils.common.MyToast;
import com.lws.basic.view.LoadingViewDialog;

import androidx.fragment.app.Fragment;
import io.reactivex.annotations.Nullable;

/**
 * 碎片的基类
 */

public abstract class BaseFragment extends Fragment {
    private LoadingViewDialog mLoadingView;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(setLayoutId(), container, false);
        initView(view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
    }

    /**
     * @return
     * 导入布局
     */
    public abstract int setLayoutId();

    protected void initView(View rootView) {

    }
    protected void initData() {
    }


    public void showLoading(){
        try {
            mLoadingView = new LoadingViewDialog();
            mLoadingView.show(getFragmentManager(), "");
        }catch (Exception e) {
            MyToast.show(e.getMessage());
        }
    }
    public void showLoading(String content){
        try {
            mLoadingView = new LoadingViewDialog(content);
            mLoadingView.show(getFragmentManager(), "");
        }catch (Exception e) {
            MyToast.show(e.getMessage());
        }

    }

    public void dismissLoading(){
        try {
            if (mLoadingView != null && mLoadingView.isAdded()) {
                mLoadingView.dismiss();
            }
        } catch (Exception e) {
            MyToast.show(e.getMessage());
        }

    }
}
